package de.tabuma.sso.user;

import java.util.*;
import de.tabuma.sso.*;
import de.tabuma.sso.sso.*;
import de.tabuma.sso.group.*;
import de.tabuma.shopping.logik.*;
import de.tabuma.sso.useruserrelation.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class UserWebservice {
    public static int create(User user, String ssoId) throws Exception {

        int groupId = UserWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user1 = UserFacade.get(sso.getUserId());
        List<UserUserRelation> uur = UserUserRelationFacade.getList(user1.getId().intValue());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user1.getName() + "/" + g1.getName(), constants.USER_CREATE, constants.SSO_SOAP_LICENSE_TYP, user1.getName());
        }        
        return UserFacade.create(user, uur.get(0).getUser());

    }

    public static int create1(User user, String ssoId, String username) throws Exception {

        int groupId = UserWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user1 = UserFacade.get(sso.getUserId());
        List<UserUserRelation> uur = UserUserRelationFacade.getList(user1.getId().intValue());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user1.getName() + "/" + g1.getName(), constants.USER_CREATE, constants.SSO_SOAP_LICENSE_TYP, user1.getName());
        }
        return UserFacade.create(user, username);
        
    }

    public static void update(User user, String ssoId) throws Exception {

        int groupId = UserWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user1 = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user1.getName() + "/" + g1.getName(), constants.USER_UPDATE, constants.SSO_SOAP_LICENSE_TYP, user1.getName());
        }

        UserWebserviceLib.userHasPrivilegForUser(user, ssoId);
        UserFacade.update(user, user1.getName());

    }
    
    public static User get(int id, String ssoId) throws Exception {

        int groupId = UserWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user1 = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user1.getName() + "/" + g1.getName(), constants.USER_GET, constants.SSO_SOAP_LICENSE_TYP, user1.getName());
        }


        User user = UserFacade.get(id);
        UserWebserviceLib.userHasPrivilegForUser(user, ssoId);
        return UserFacade.get(id);

    }

    public static void remove(int id, String ssoId) throws Exception {

        int groupId = UserWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user1 = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user1.getName() + "/" + g1.getName(), constants.USER_REMOVE, constants.SSO_SOAP_LICENSE_TYP, user1.getName());
        }
        
        User user = UserFacade.get(id);
        UserFacade.remove(id, user1.getName());
    }
}